package cn.net.xyan.yypt.demo

import cn.net.xyan.blossom.kotlin.DateExt
import cn.net.xyan.blossom.kotlin.javaType
import cn.net.xyan.blossom.kotlin.json
import cn.net.xyan.blossom.kotlin.parametricType
import cn.net.xyan.yypt.service.PayOrderService
import cn.net.xyan.yypt.dto.protal.LKAlipayEmptyBodyReq
import cn.net.xyan.yypt.dto.protal.LKAlipayQRCode
import cn.net.xyan.yypt.dto.protal.LKAlipayRsp
import cn.net.xyan.yypt.dto.protal.QrcodeTradePayResultBody
import com.fasterxml.jackson.dataformat.xml.XmlMapper
import java.net.URL
import java.text.SimpleDateFormat


const val callback = "{\"biz_content\":\"{\\\"out_trade_no\\\":\\\"20180112170247792\\\",\\\"trade_no\\\":\\\"2018011221001004400276483566\\\",\\\"user_id\\\":\\\"2088002382669404\\\",\\\"total_amount\\\":\\\"0.01\\\",\\\"trade_status\\\":\\\"TRADE_SUCCESS\\\"}\",\"nonce_str\":\"65e94647-3b23-44a3-894e-1193e7e546f0\",\"method\":\"lc.trade.notify\",\"sign\":\"Z%2bTB0EVXGWdG8iX9rZ0RbrPPsSBaZ06Mqv%2fu4ggwCcWCEH9tEpa35Do3PFoXQ1%2b19caUhYwUrSVZNcdbSUshqrpsqka2gjY7%2frP6hdSOk1tAUEwMIvISrLlBgQQBiPIQkhCrRptnJEw4iE4dfkiKhoX%2fikZWKGgaQYFnqb1stmk%3d\",\"app_id\":\"LCOP20171228012536079\",\"sign_type\":\"rsa\",\"timestamp\":\"2018-01-12 17:03:02\"}"

fun main(array: Array<String>) {

    val BaseURL = "http://yypt.xyan.net.cn:18002/ydpt/public/qrCode?value="

    val df = SimpleDateFormat("yyyyMMddHHmmssSSS")

    val xmlMapper = XmlMapper()

    println(xmlMapper.writeValueAsString("abc"))

    val url = URL("http://yypt.xyan.net.cn:18002/ydpt/public/cxf/pay?wsdl")

    // val url = URL("http://127.0.0.1:8080/public/cxf/pay?wsdl")

    val service: PayOrderService = createInstance(url, PayOrderService::class.java)


    val qrCodeHead = LKAlipayQRCode()

    qrCodeHead.outTradeNo = df.format(DateExt.now)

    qrCodeHead.totalAmount = "0.01"

    qrCodeHead.caption = "测试"

    qrCodeHead.descriptive = "test"

    qrCodeHead.tradeDetail = "detail"

    val req = LKAlipayEmptyBodyReq<LKAlipayQRCode>()

    println(qrCodeHead.json)

    req.head = qrCodeHead


    val rsp = service.createPayOrder(xmlMapper.writeValueAsString(req))

    println(rsp)


    val rspObj: LKAlipayRsp<QrcodeTradePayResultBody> = xmlMapper.readValue(rsp, LKAlipayRsp::class.parametricType(QrcodeTradePayResultBody::class.javaType))

    val biz_content = rspObj?.body?.qrcodeTradePayResult
    val qrCode = biz_content?.twoDimensionCode
    println(biz_content?.twoDimensionCode)

    println("$BaseURL$qrCode")


}